const Promise = require('bluebird');
const apis = {};

const withdrawV1 = require('./v1/withdraw');
const suggestionV1 = require('./v1/suggestion');
const accountsV1 = require('./v1/accounts');
const ordersV1 = require('./v1/orders');
const userOrdersV1 = require('./v1/user_orders');
const jdOrdersV1 = require('./v1/jdorders');
const agencysV1 = require('./v1/agencys');
const settlesV1 = require('./v1/settles');
Object.assign(apis, {
    /**
     * 建议反馈
     */
    '/v1/suggestion/save': {
        permission: true,
        handler: suggestionV1.save
    },
    /**
     * 提现
     */
    '/v1/withdraw/apply': {
        permission: true,
        handler: withdrawV1.apply
    },
    '/v1/withdraw/status': {
        permission: true,
        handler: withdrawV1.status
    },
    /**
     * 账户
     */
    '/v1/accounts/find': {
        permission: true,
        handler: accountsV1.find
    },
    '/v1/accounts/save': {
        permission: true,
        handler: accountsV1.save
    },
    '/v1/accounts/failed': {
        permission: true,
        handler: accountsV1.failed
    },
    /**
     * 订单
     */
    '/v1/orders/save': {
        permission: true,
        handler: ordersV1.save,
        table: 'TaokeOrders'
    },
    '/v1/orders/refund_save': {
        permission: true,
        handler: ordersV1.refundSave,
        table: 'TaokeRefunds'
    },
    '/v1/orders/operate': {
        permission: true,
        handler: ordersV1.operate
    },
    '/v1/orders/orders_analyse': {
        permission: true,
        handler: ordersV1.ordersAnalyse
    },
    '/v1/orders/orders_frozen': {
        permission: true,
        handler: ordersV1.ordersFrozen
    },
    '/v1/jdorders/save': {
        permission: true,
        handler: jdOrdersV1.save,
        table: 'JDOrders'
    },
    /**
     * 订单
     */
    '/v1/user_orders/operate': {
        permission: true,
        handler: userOrdersV1.operate
    },
    /**
     * 管理
     */
    '/v1/agencys/settle': {
        permission: true,
        handler: agencysV1.settle
    },
    '/v1/agencys/batch_settle': {
        permission: true,
        handler: agencysV1.batchSettle
    },
    '/v1/settles/handle': {
        permission: true,
        handler: settlesV1.handle
    },
    '/v1/settles/batch_handle': {
        permission: true,
        handler: settlesV1.batchHandle
    }
});

module.exports = function (storage) {
    return function (req, res) {
        let api = apis[req.path];
        if (api) {
            let {handler, permission, table} = api;
            let user_id = req.session.user_id;
            //权限处理
            if (permission && !user_id) {
                return res.status('401').json({success: false, code: 401, message: '未登录'});
            }

            storage.controller.handleBody({
                req: req,
                model: table && (storage.models[table] || storage.modelsData[table])
            });
            return Promise.resolve().then(function () {
                return handler({
                    req: req,
                    res: res,
                    body: req.body,
                    query: req.query,
                    session: req.session,
                    user_id: user_id
                }, storage);
            }).then(function (response) {
                return res.json(response);
            }, function (err) {
                if (err) {
                    console.error('\n', err.stack || err.message);
                }
                return res.json({success: false, code: 500, message: err.message || '请求异常'});
            });
        } else {
            return res.json({success: false, code: 404, message: '无效API'});
        }
    }
}